package com.chenjl.generator.generated.impl;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

import com.chenjl.generator.Generator;
import com.chenjl.generator.generated.AbstractGenerated;
/**
 * 按照id倒序排列取第一个
 * 2020-4-8 15:05:58
 * @author chenjinlong
 */
public class SelectLastSelectiveGenerated implements AbstractGenerated {
	
	
	@Override
	public Method clientGenerated(String baseRecordType) {
		//增加selectLastSelective方法
		Parameter parameter = new Parameter(new FullyQualifiedJavaType(baseRecordType),Generator.QUERY_VAR_NAME);
		parameter.addAnnotation("@Param(\""+Generator.QUERY_VAR_NAME+"\")");
		
		Method selectLastSelectiveMethod = new Method();
		selectLastSelectiveMethod.addJavaDocLine("/**");
		selectLastSelectiveMethod.addJavaDocLine(" * 通用查询方法，按照id倒叙排列取第一个元素");
		selectLastSelectiveMethod.addJavaDocLine(" */");
		selectLastSelectiveMethod.setName("selectLastSelective");
		selectLastSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
		selectLastSelectiveMethod.setReturnType(new FullyQualifiedJavaType(baseRecordType));
		selectLastSelectiveMethod.addParameter(parameter);
		return selectLastSelectiveMethod;
	}

	@Override
	public XmlElement sqlMapDocumentGenerated(IntrospectedTable introspectedTable,String tableName) {
		XmlElement include = new XmlElement("include");
		include.addAttribute(new Attribute("refid","Base_Column_List")); 
		
		/**
		 * <where >  
		 * <if test="accountId != null" >  
		 *   and account_id = #{accountId,jdbcType=INTEGER}  
		 * </if>  
		 * </where>
		 */
		XmlElement whereXmlElement = new XmlElement("where");
		for(IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
			//属性条件
			XmlElement isNotNullElement = new XmlElement("if");
			isNotNullElement.addAttribute(new Attribute("test",Generator.QUERY_VAR_NAME+"."+introspectedColumn.getJavaProperty()+" != null"));
			
			StringBuilder sb = new StringBuilder();
			sb.append(" and ");
			sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
			sb.append(" = ");
			sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn,Generator.QUERY_VAR_NAME+"."));
			isNotNullElement.addElement(new TextElement(sb.toString()));
			
			whereXmlElement.addElement(isNotNullElement);
		}
		
		XmlElement selectLastSelectiveXmlElement = new XmlElement("select");
		selectLastSelectiveXmlElement.addAttribute(new Attribute("id", "selectLastSelective"));
		selectLastSelectiveXmlElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
		selectLastSelectiveXmlElement.addElement(new TextElement("select "));
		selectLastSelectiveXmlElement.addElement(include);
		selectLastSelectiveXmlElement.addElement(new TextElement(" from "+tableName));
		selectLastSelectiveXmlElement.addElement(whereXmlElement);
		selectLastSelectiveXmlElement.addElement(new TextElement(" order by id desc limit 0,1"));
		
		return selectLastSelectiveXmlElement;
	}
}